<?php
declare (strict_types = 1);

namespace app\admin\model\user;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class User extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $id
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id){
        return $this->where('id',$id)->find();
    }

    /**
     * 获取列表
     * @param $getData
     * @param $page
     * @param $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getList($getData,$page,$limit){
        $sql = $this;
        if(isset($getData['id']) && $getData['id']) $sql = $sql->whereLike('id',$getData['id']);
        if(isset($getData['nickname']) && $getData['nickname']) $sql = $sql->whereLike('nickname','%'.$getData['nickname'].'%');
        if(isset($getData['account']) && $getData['account']) $sql = $sql->whereLike('account','%'.$getData['account'].'%');
        $count = $sql->count();
        $statusType = [0=>'禁止',1=>'正常'];
        $data = $sql->page($page,$limit)->order('id','desc')->select()->each(function ($item)use($statusType){
            $item['status_name'] = $statusType[$item['status']];
            $item['pay_pwd_status'] = $item['pay_pwd_status'] ? '是' : '否';
            return $item;
        })->toArray();
        return compact('count','data');
    }

    /**
     * 修改用户信息
     * @param $saveData
     * @return static
     */
    function editData($saveData){
        $updateData = [
            'mark'=>$saveData['mark'],
            'status'=>$saveData['status'],
        ];
        if(isset($saveData['log_pwd']) && $saveData['log_pwd']) $updateData['log_pwd'] = password_hash($saveData['log_pwd'], PASSWORD_BCRYPT);
        if(isset($saveData['pay_pwd']) && $saveData['pay_pwd']) $updateData['pay_pwd'] = password_hash($saveData['pay_pwd'], PASSWORD_BCRYPT);
        return $this->where('id',$saveData['id'])->update($updateData);
    }

    /**
     * 修改用户账户
     * @param $getData
     * @return static
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function updateBalance($getData,$userInfo){
        $userBillModel = new UserBill();
        //增加账户余额
        if($getData['update_type'] == 1){
            $billData = [
                'uid'=>$userInfo['id'],
                'link_id'=>0,
                'pm'=>1,
                'title'=>'后台增加余额',
                'category'=>'now_money',
                'type'=>'admin_set_balance',
                'number'=>$getData['money'],
                'balance'=>$userInfo['now_money'],
                'mark'=>'后台增加余额'.$getData['money'],
                'create_time'=>date("Y-m-d H:i:s"),
                'status'=>1,
            ];

            Db::startTrans();
            $userResult = $this->where('id',$userInfo['id'])->inc('now_money',$getData['money'] * 1)->update();
            $userBillResult = $userBillModel->insertGetId($billData);
            if($userResult && $userBillResult){
                Db::commit();
                return ['code'=>0,'设置成功'];
            }else{
                Db::rollback();
                return ['code'=>1,'设置失败，请重试'];
            }
        //减少账户余额
        }else if($getData['update_type'] == 2){
            $billData = [
                'uid'=>$userInfo['id'],
                'link_id'=>0,
                'pm'=>0,
                'title'=>'后台减少余额',
                'category'=>'now_money',
                'type'=>'admin_set_balance',
                'number'=>$getData['money'],
                'balance'=>$userInfo['now_money'],
                'mark'=>'后台减少余额'.$getData['money'],
                'create_time'=>date("Y-m-d H:i:s"),
                'status'=>1,
            ];
            Db::startTrans();
            $userResult = $this->where('id',$userInfo['id'])->dec('now_money',$getData['money'] * 1)->update();
            $userBillResult = $userBillModel->insertGetId($billData);
            if($userResult && $userBillResult){
                Db::commit();
                return ['code'=>0,'设置成功'];
            }else{
                Db::rollback();
                return ['code'=>1,'设置失败，请重试'];
            }
        }else{
            return ['code'=>1,'msg'=>'请选择正确的余额设置方式'];
        }
    }


}
